
** Set directories 

cd "/work/sf/internal/l1werp20.sf.frb.org/Shared/Olivia/Rob/Covid_Unemployment_Paths"

tempfile hh hhfam 

** Bring in 2020 HH-level ASEC

import delimited "Input/hhpub20.csv", stringcols(1) clear 

keep h_seq hsup_wgt hearnval htotval gestfips h_numper h_idnum

save `hh'

** Bring in 2020 family-level ASEC 

import delimited "Input/ffpub20.csv", clear 

keep ffpos fh_seq fsup_wgt fearnval ftotval
rename fh_seq h_seq

merge m:1 h_seq using `hh', nogen keep(3)

tostring h_seq, format(%05.0f) replace
tostring ffpos, format(%02.0f) replace
gen hh_fam = h_seq + ffpos

save `hhfam'

** Bring in 2020 person-level ASEC

import delimited "Input/pppub20.csv", stringcols(1) clear 

tostring ph_seq, format(%05.0f) replace
tostring pf_seq, format(%02.0f) replace
gen hh_fam = ph_seq + pf_seq

merge m:1 hh_fam using `hhfam', nogen keep(3)

tostring(a_lineno), replace format(%02.0f)

drop if p_stat == 2

keep peridnum prdtrace a_hga a_sex a_age wkswork wewkrs ptweeks hrswk ///
marsupwt ptotval pearnval wemind wemocg lkweeks rsnnotw workyn hsup_wgt fsup_wgt ///
hearnval htotval fearnval ftotval prcitshp gestfips pemlr hh_fam h_seq h_numper a_lineno h_idnum

** Merge in CPS data

merge 1:m peridnum using "Intermediate/cps_match.dta", nogen keep(3)

** Check IDs match

assert hhidtest == h_idnum
assert a_lineno == lineno

drop if a_age <= 15

bysort peridnum (month): gen ok = ///
(month - month[_n-1] == 1) | (month[_n+1] - month == 1) 
drop if ok == 0 
drop ok

bysort peridnum: gen obs = _N
drop if obs < 2
gen balanced = obs == 4

gen oversample = (month == 2 & (mis == 4 | mis == 8)) ///
| (month == 4 & (mis == 1 | mis == 5))

** Gen merge flags

gen byte fl_sex_asec=(a_sex-female != 1) 
gen byte fl_race_asec=(race6!=prdtrace) 
gen byte fl_age_asec=(age-a_age > 1 | age-a_age < -1) 
gen byte fl_educ_asec=0
  replace fl_educ_asec=1 if grdatn==31 & a_hga != 31
  replace fl_educ_asec=1 if grdatn==32 & a_hga != 32
  replace fl_educ_asec=1 if grdatn==33 & a_hga != 33
  replace fl_educ_asec=1 if grdatn==34 & a_hga != 34
  replace fl_educ_asec=1 if grdatn==35 & a_hga != 35
  replace fl_educ_asec=1 if grdatn==36 & a_hga != 36
  replace fl_educ_asec=1 if grdatn==37 & (a_hga<36 | a_hga>39)
  replace fl_educ_asec=1 if grdatn==38 & (a_hga<37 | a_hga>42)
  replace fl_educ_asec=1 if grdatn==39 & (a_hga<38 | a_hga>42)
  replace fl_educ_asec=1 if grdatn==40 & (a_hga<38 | a_hga>43)
  replace fl_educ_asec=1 if grdatn==41 & (a_hga<40 | a_hga>43)
  replace fl_educ_asec=1 if grdatn==42 & (a_hga<40 | a_hga>43)
  replace fl_educ_asec=1 if grdatn==43 & (a_hga<40)
  replace fl_educ_asec=1 if grdatn==44 & (a_hga<43)
  replace fl_educ_asec=1 if grdatn==45 & (a_hga<43)
  replace fl_educ_asec=1 if grdatn==46 & (a_hga<43)
gen fl_bad_asec=(fl_sex_asec==1|fl_race_asec==1|fl_age_asec==1|fl_educ_asec==1)

drop a_age a_sex a_hga pemlr gestfips race6 hhidtest obs lineno hh_fam h_seq h_numper prdtrace

replace citizen = 0 if prcitshp == 5

** Gen earnings variables
gen weekly_earnings = pearnval/wkswork
replace weekly_earnings = 0 if missing(weekly_earnings)

gen wage = weekly_earnings/hrswk
replace wage = 0 if missing(wage)

gen undr_minwge = wage < 7.25
replace undr_minwge = . if wage <= 0

save "Intermediate/cps_asec_merged.dta", replace

** Output csv for UI calculator

drop if (st == "DC") | (citizen == 0) | (weekly_earnings <= 0) | (hrswk <= 0) | (undr_minwge == 1)
keep weekly_earnings wkswork id st
duplicates drop

foreach quarter of num 1/4 {
	gen q`quarter'_weeks = wkswork - 52 + 13 * (`quarter') 
	
	gen q`quarter' = q`quarter'_weeks * weekly_earnings
	replace q`quarter' = 0 if q`quarter'_weeks < 0 
	replace q`quarter' = 13*weekly_earnings if q`quarter'_weeks > 13 
	
	drop q`quarter'_weeks
}

export delimited using "Intermediate/python.csv", replace
